﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using CDMS.Common;
using DevExpress.XtraTreeList.Columns;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraEditors.Controls;
using CDMS.BLL;
using System.Configuration;
using DevExpress.LookAndFeel;
using CDMS.Utilities;

namespace CDMS
{
    public partial class ucCustomerList : DevExpress.XtraEditors.XtraUserControl
    {
        int CustomerID = 0;
        string caption;
        int CustomerGroupID = 0;
        public ucCustomerList(string caption)
        {
            InitializeComponent();
            this.caption = caption;

            TreeListDataSource.CustomerGroup(treeList1);
            treeList1.FocusedNodeChanged += treeList1_FocusedNodeChanged;

            AddColumn();
            LoadData();
        }

        void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            if (e.Node == null)
                return;
            string sNodename = e.Node.GetDisplayText("CustomerGroup_ID");
            CustomerGroupID = Convert.ToInt32(e.Node.GetValue("CustomerGroup_ID"));
            LoadData();
        }
     

        private void AddColumn()
        {
            gridView1.Columns.Clear();
            
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_ID", "", "", 0, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_Code", "Mã", "", 80, true, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_Name", "Khách hàng", "", 100, true, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_NameEN", "Tên tiếng Anh", "", 100, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_DOB", "Ngày lập", "", 100, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_Address", "Địa chỉ", "", 250, true, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_AddressShip", "Địa chỉ giao hàng", "", 300, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_Phone", "Điện thoại", "", 100, true, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_Mobile", "Di động", "", 100, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_Email", "Email", "", 100, true, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_Contact", "Liên hệ", "", 100, true, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_ContactPhone", "Điện thoại liên hệ", "", 100, true, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_Position", "", "", 100, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_Vocative", "", "", 100, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_CustomerGroupID", "", "", 100, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_CustomerTypeID", "", "", 100, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_CityID", "", "", 100, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_SourceInfoID", "", "", 100, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_UserID", "", "", 100, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_NationlatyID", "", "", 100, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_MarketSegmentID", "", "", 100, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_RegistrationNo", "Số ĐKKD", "", 100, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_RegistrationName", "Tên ĐKKD", "", 100, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_RegistrationAddress", "Địa chỉ ĐKKD", "", 100, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_TaxCode", "Mã số thuế", "", 100, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_AcceptEmail", "Email chấp nhận", "", 100, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_Website", "Website", "", 100, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Position_Name", "Vị trí", "", 100, true, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Vocative_Name", "Xưng hô", "", 100, true, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "CustomerType_Name", "", "", 100, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "City_Name", "", "", 100, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "SourceInfo_Name", "", "", 100, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Users_Name", "", "", 100, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Nationlaty_Name", "", "", 100, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "MarketSegment_Name", "", "", 100, false, true);

        }


        void LoadData()
        {
            FormGlobals.ShowWaitForm();
            Customer_BLL Customer_BLL = new Customer_BLL();
            DataTable dt = Customer_BLL.GetAllByCustomerGroupID(CustomerGroupID);
            gridControl1.DataSource = dt;
            gridControl1.ForceInitialize();
            FormGlobals.CloseWaitForm();
        }

        private void barButtonItemAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Forms.frmCustomerInfo frm = new Forms.frmCustomerInfo(0);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }
        }

        private void barButtonItemEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0)
                return;
            CustomerID = (int)gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[0]);
            Forms.frmCustomerInfo frm = new Forms.frmCustomerInfo(CustomerID);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }
        }

        private void barButtonItemDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0)
                return;

            if (FormGlobals.Message_QuestionYesNo("Bạn có chắc chắn muốn xóa dòng này?", "Xác nhận xóa") == DialogResult.Yes)
            {

                CustomerID = (int)gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[0]);
                Customer_BLL Customer_BLL = new Customer_BLL();
                if (Customer_BLL.Delete(CustomerID))
                    LoadData();
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnExportExcel_Click(object sender, EventArgs e)
        {
            FormGlobals.ExportDataToExcel(gridControl1, caption);
        }

        private void ucCustomerList_BackColorChanged(object sender, EventArgs e)
        {
            
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            barButtonItemEdit.PerformClick();
        }
    }
}
